package com.yass.zaika;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

public class ItemAdapter extends BaseAdapter implements Filterable {

	private List<Item> row_states;
	private final Activity context;
	private List<String> arrayList;
	private List<String> mOriginalValues;
	private HashMap<String, Boolean> table;

	static class ViewHolder {
		protected TextView text;
		protected CheckBox checkbox;
	}

	public ItemAdapter(Activity context, List<Item> r_s, List<String> i_l) {
		row_states = r_s;
		this.context = context;
		arrayList = i_l;

		table = new HashMap<String, Boolean>(row_states.size());

		for (int i = 0; i < row_states.size(); i++) {
			table.put(row_states.get(i).getString(), row_states.get(i)
					.getChecked());
		}
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return row_states.size();
	}

	@Override
	public Item getItem(int arg0) {
		// TODO Auto-generated method stub
		return row_states.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		// TODO Auto-generated method stub

		View view = null;
		if (convertView == null) {
			LayoutInflater inflator = context.getLayoutInflater();
			view = inflator.inflate(R.layout.row_layout, null);
			final ViewHolder viewHolder = new ViewHolder();
			viewHolder.text = (TextView) view.findViewById(R.id.label);
			viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);

			viewHolder.checkbox
					.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

						@Override
						public void onCheckedChanged(CompoundButton buttonView,
								boolean isChecked) {
							Item element = (Item) viewHolder.checkbox.getTag();
							element.setChecked((buttonView.isChecked()));
							table.remove(element.getString());
							table.put(element.getString(), element.getChecked());
						}
					});

			view.setTag(viewHolder);
			viewHolder.checkbox.setTag(row_states.get(position));
		} else {
			view = convertView;
			((ViewHolder) view.getTag()).checkbox.setTag(row_states
					.get(position));
		}
		ViewHolder holder = (ViewHolder) view.getTag();
		holder.text.setText(row_states.get(position).getString());
		holder.checkbox.setChecked(row_states.get(position).getChecked());
		return view;
	}

	@Override
	public Filter getFilter() {
		// TODO Auto-generated method stub
		Filter filter = new Filter() {

			@Override
			protected FilterResults performFiltering(CharSequence constraint) {
				// TODO Auto-generated method stub

				FilterResults results = new FilterResults(); // Holds the
																// results of a
																// filtering
																// operation in
																// values
				List<String> FilteredArrList = new ArrayList<String>();

				if (mOriginalValues == null) {
					mOriginalValues = new ArrayList<String>(arrayList); // saves
																		// the
																		// original
																		// data
																		// in
																		// mOriginalValues
				}

				/********
				 * 
				 * If constraint(CharSequence that is received) is null returns
				 * the mOriginalValues(Original) values else does the Filtering
				 * and returns FilteredArrList(Filtered)
				 * 
				 ********/
				if (constraint == null || constraint.length() == 0) {

					// set the Original result to return
					results.count = mOriginalValues.size();
					results.values = mOriginalValues;
				} else {
					constraint = constraint.toString().toLowerCase();
					for (int i = 0; i < mOriginalValues.size(); i++) {
						String data = mOriginalValues.get(i);
						if (data.toLowerCase()
								.startsWith(constraint.toString())) {
							FilteredArrList.add(data);
						}
					}
					// set the Filtered result to return
					results.count = FilteredArrList.size();
					results.values = FilteredArrList;
				}
				return results;
			}

			@SuppressWarnings("unchecked")
			@Override
			protected void publishResults(CharSequence s, FilterResults values) {
				// TODO Auto-generated method stub

				if (s.equals("")) {
					arrayList = mOriginalValues;
				}

				else
					arrayList = (List<String>) values.values;

				row_states.clear();

				for (int i = 0; i < arrayList.size(); i++) {
					row_states.add(new Item(arrayList.get(i), table
							.get(arrayList.get(i))));
				}
				if (values.count > 0) {

					notifyDataSetChanged();
				}

				else {
					notifyDataSetInvalidated();
				}
			}

		};

		return filter;
	}

}
